﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OLMS.DAL;

namespace OLMS.BLL
{
    public class OrderBLL
    {
        //订单状态分为0：未付款 1：待发货 2：已发货 3：确认收货 4：退货 
        public static List<Orders> GetOrdersList(string phone)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Orders.Include("OrderDetails").Where(o => o.Phone == phone).ToList();
            }
        }
        public static List<Orders> GetAllOrdersList()
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Orders.Include("OrderDetails").ToList();
            }
        }

        public static bool UpdateOrderStatus(int orderId, int orderStatus)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Orders orders = db.Orders.SingleOrDefault(o => o.OrderId == orderId);
                if (orders != null)
                {
                    orders.OrderStatus = orderStatus;
                    return db.SaveChanges() > -1 ? true : false;
                }
                else
                {
                    return false;
                }
            }
        }
        //根据订单编号查询订单信息
        public static Orders GetOrdersById(int orderId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                if (orderId == 0)
                {
                    return new Orders
                    {
                        OrderId = 0,
                        Username = "",
                        Phone = "",
                        Email = "",
                        Address = "",
                        PostalCode = "",
                        OrderDate=DateTime.Now,
                        Total = 0
                    };
                }
                else
                {
                    return db.Orders.SingleOrDefault(o => o.OrderId == orderId);
                }
            }
        }
        //根据订单编号查询订单详细信息
        public static List<OrderDetails> GetOrderDetailsByOrderId(int orderId,int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                if (orderId == 0)
                {
                    var list = db.Carts.Where(c => c.UserId == userId).ToList();
                    return list.Select(c => new OrderDetails
                    {
                        OrderDetailId = 0,
                        OrderId = 0,
                        AlbumId = c.AlbumId,
                        Quantity = c.Count,
                        UnitPrice = c.Albums.Price,
                    }).ToList();
                }
                else
                {
                    return db.OrderDetails.Where(od => od.OrderId == orderId).ToList();
                }

            }
        }
        //添加订单
        public static bool AddOrder(int userId, Orders orders)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                bool result = false;
                db.Orders.Add(orders);
                result = db.SaveChanges() != -1 ? true : false;
                List<Carts> list = CartBLL.GetCarts(userId);
                foreach (Carts carts in list)
                {
                    OrderDetails orderDetails = new OrderDetails();
                    orderDetails.OrderId = orders.OrderId;
                    orderDetails.AlbumId = carts.AlbumId;
                    orderDetails.UnitPrice = carts.Albums.Price;
                    orderDetails.Quantity = carts.Count;
                    db.OrderDetails.Add(orderDetails);
                }
                result = db.SaveChanges() != -1 ? true : false;
                CartBLL.EmptyCart(userId);
                return result;
            }
        }
    }


}